<?php

class ProductsController extends AppController{
    public $helpers = array ('Html', 'Form');
    public $components  = array ('Session');
    
    public function index(){
        $this->Product->recursive = 1;
        $params = array(
           // 'order' => 'nombre asc'
            );
        $this->set('productos',$this->Product->find('all', $params));
    }
    
    public function add(){
        if($this->request->is('post')):
            if($this->Product->save($this->request->data)):
                $this->Session->setFlash('Producto Guardado');
                $this->redirect(array('action'=>'index'));
            endif;
        endif;     
       $promotions = $this->Product->Promotion->find('list');
       $this->set(compact('promotions'));
    }
    
    public function edit($id = null){
        $this->Product->id = $id;
        if($this->request->is('get')):
        // lee los datos de la peticion y los coloca en las cajas
            $this->request->data = $this->Product->read();
        else:
            if($this->Product->save($this->request->data)):
                $this->Session->setFlash('Producto Guardado');
                $this->redirect(array ('action' => 'index'));
        else:
            $this->Session->setFlash('No se pudo guardar');
        endif;
      endif;
       $promotions = $this->Product->Promotion->find('list');
       $this->set(compact('promotions'));
      
    }
    
    public function delete($id){
        if($this->request->is('get')):
        throw new methodNotAllowedException();
        else:
            if($this->Product->delete($id)):
        $this->Session->setFlash('Producto Eliminado');
        $this->redirect(array('action'=>'index'));
            endif;
        endif;     

    }
}
